package cn.sunlelin.educationserver.controller;

import cn.sunlelin.educationserver.entity.Appraise;
import cn.sunlelin.educationserver.service.AppraiseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class AppraiseController {
    @Autowired
    private AppraiseService appraiseService;

    @PostMapping("/appraise/createAppraise")
    public Map<String,Object> createAppraise(@RequestBody Appraise appraise){
        Map<String,Object> map=new HashMap<>();
        if(appraiseService.createAppraise(appraise)){
            map.put("code",200);
            map.put("msg","添加评论成功");
            map.put("dataobject",null);
        }
        else {
            map.put("code",500);
            map.put("msg","添加评论失败，你已经评价过该课程");
            map.put("dataobject",null);
        }
        return map;
    }

    @PostMapping("/appraise/updateAppraise")
    public Map<String,Object> updateAppraise(@RequestBody Appraise appraise){
        Map<String,Object> map=new HashMap<>();
        if(appraiseService.updateAppraise(appraise)){
            map.put("code",200);
            map.put("msg","修改评论成功");
            map.put("dataobject",null);
        }
        else {
            map.put("code",500);
            map.put("msg","修改评论失败，没有此条评论记录");
            map.put("dataobject",null);
        }
        return map;
    }

    @GetMapping("/appraise/selectAppraiseByCourseId/{courseId}")
    public Map<String,Object> selectAppraiseByCourseId(@PathVariable Integer courseId){
        List<Map<String,Object>> appraiseList=appraiseService.selectAppraiseByCourseId(courseId);
        Map<String,Object> map=new HashMap<>();
        if(appraiseList!=null){
            map.put("code",200);
            map.put("msg","根据courseId查询评论成功");
            map.put("dataobject",appraiseList);
        }
        else {
            map.put("code",500);
            map.put("msg","根据courseId查询评论失败");
            map.put("dataobject",null);
        }
        return map;
    }

    @GetMapping("/appraise/selectAppraiseByUserId/{userId}")
    public Map<String,Object> selectAppraiseByUserId(@PathVariable Integer userId){
        List<Appraise> appraiseList=appraiseService.selectAppraiseByUserId(userId);
        Map<String,Object> map=new HashMap<>();
        if(appraiseList!=null){
            map.put("code",200);
            map.put("msg","根据userId查询评论成功");
            map.put("dataobject",appraiseList);
        }
        else {
            map.put("code",500);
            map.put("msg","根据userId查询评论失败");
            map.put("dataobject",null);
        }
        return map;
    }

    @GetMapping("/appraise/selectAppraiseById/{appraiseId}")
    public Map<String,Object> selectAppraiseById(@PathVariable Integer appraiseId){
        Appraise appraise=appraiseService.selectAppraiseById(appraiseId);
        Map<String,Object> map=new HashMap<>();
        if(appraise!=null){
            map.put("code",200);
            map.put("msg","根据Id查询评论成功");
            map.put("dataobject",appraise);
        }
        else {
            map.put("code",500);
            map.put("msg","根据Id查询评论失败");
            map.put("dataobject",null);
        }
        return map;
    }

    @PostMapping("/appraise/selectAppraiseByCourseAndUser")
    public Map<String,Object> selectAppraiseByCourseAndUser(@RequestBody Appraise appraise){
        Appraise appraiseOut=appraiseService.selectAppraiseByCourseAndUser(appraise);
        Map<String,Object> map=new HashMap<>();
        if(appraiseOut!=null){
            map.put("code",200);
            map.put("msg","根据CourseId和UserId查询评论成功");
            map.put("dataobject",appraiseOut);
        }
        else {
            map.put("code",500);
            map.put("msg","根据CourseId和UserId查询评论失败");
            map.put("dataobject",null);
        }
        return map;
    }

    @GetMapping("/appraise/deleteAppraise/{appraiseId}")
    public Map<String,Object> deleteAppraise(@PathVariable Integer appraiseId){
        Map<String,Object> map=new HashMap<>();
        if(appraiseService.deleteAppraise(appraiseId)){
            map.put("code",200);
            map.put("msg","删除评论成功");
            map.put("dataobject",null);
        }
        else {
            map.put("code",500);
            map.put("msg","删除评论失败，此条评论不存在");
            map.put("dataobject",null);
        }
        return map;
    }
}
